﻿using CheckIn.ToolsHost.Applications;
using CheckIn.ToolsHost.Infrastructures;
using JMS;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using Way.Lib;
namespace CheckIn.ToolsHost
{
    public class Program
    {

        static void Main(string[] args)
        {
            Run();
        }

        public static void Run(string appsettingPath = "appsettings.json", Action<ServiceCollection> initServiceAction = null)
        {

            Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory; 


            var builder = new ConfigurationBuilder();
            builder.AddJsonFile(appsettingPath, optional: true, reloadOnChange: true);
            Global.Configuration = builder.Build();

            var port = Global.Configuration.GetValue<int>("Port"); //提供微服务的端口
            var serviceAddress = Global.Configuration.GetValue<string>("ServiceAddress");

            //网关地址
            Global.GatewayAddresses = Global.Configuration.GetSection("Gateways").Get<NetAddress[]>();

            ServiceCollection services = InitServices();

            var msp = new MicroServiceHost(services);
            if (string.IsNullOrEmpty(serviceAddress) == false)
            {
                //自定义微服务地址
                msp.ServiceAddress = new NetAddress(serviceAddress, port);
            }
            msp.Register<Controllers.MainController>("ToolsService", "道家工具", true);
            msp.ServiceProviderBuilded += Msp_ServiceProviderBuilded;

            initServiceAction?.Invoke(services);

            msp.Build(port, Global.GatewayAddresses, true)
                .Run();
        }

        static ServiceCollection InitServices()
        {
            ServiceCollection services = new ServiceCollection();
            services.AddSingleton<IConfiguration>(Global.Configuration);
            services.AddScoped<SystemDBContext>();
            services.AddSingleton<StrokeCountService>();
            services.AddSingleton<NumberResultService>();
            services.AddSingleton<FortuneTellingService>();
            services.AddSingleton<FiveElementService>();
             

            //使用Serilog处理日志
            Global.InitSerilogLog(services);

            return services;
        }

        public static IServiceProvider GetServiceProvider()
        {
            while (Global.ServiceProvider == null)
                System.Threading.Thread.Sleep(100);
            return Global.ServiceProvider;
        }

        private static void Msp_ServiceProviderBuilded(object sender, IServiceProvider e)
        {
            Global.ServiceProvider = e;
            var logger = Global.ServiceProvider.GetRequiredService<ILogger<Program>>();

#if DEBUG
            var tg = ChineseEraConverter.GetMonthEra(new DateTime(2015 , 11 , 9));
            var test = Global.ServiceProvider.GetService<FortuneTellingService>().FortuneTelling("阮", "煜衡");
            test.MonthDizhi = tg.DiZhiString;
            test.MonthFiveElement = Global.ServiceProvider.GetService<FiveElementService>().GetFiveElementByChinese(tg.DiZhiString);
            var json = test.ToJsonString(true);
#endif


            logger.LogInformation("启动完毕");
        }
    }
}
